package com.syp.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syp.lease.model.entity.*;
import com.syp.lease.web.admin.mapper.*;
import com.syp.lease.web.admin.service.LeaseAgreementService;
import com.syp.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.syp.lease.web.admin.vo.agreement.AgreementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 租约服务实现类
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    // 注入公寓信息Mapper
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    // 注入房间信息Mapper
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    // 注入支付类型Mapper
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    // 注入租赁期限Mapper
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    /**
     * 根据ID获取租约详细信息
     *
     * @param id 租约ID
     * @return 租约详细信息
     */
    @Override
    public AgreementVo getAgreementById(Long id) {
        // 根据ID查询租约基本信息
        LeaseAgreement agreement = baseMapper.selectById(id);
        // 根据公寓ID查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(agreement.getApartmentId());
        // 根据房间ID查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(agreement.getRoomId());
        // 根据支付类型ID查询支付类型信息
        PaymentType paymentType = paymentTypeMapper.selectById(agreement.getPaymentTypeId());
        // 根据租赁期限ID查询租赁期限信息
        LeaseTerm leaseTerm = leaseTermMapper.selectById(agreement.getLeaseTermId());

        // 创建租约视图对象
        AgreementVo agreementVo = new AgreementVo();
        // 将租约基本信息复制到视图对象
        BeanUtils.copyProperties(agreement, agreementVo);
        // 设置公寓信息
        agreementVo.setApartmentInfo(apartmentInfo);
        // 设置房间信息
        agreementVo.setRoomInfo(roomInfo);
        // 设置支付类型信息
        agreementVo.setPaymentType(paymentType);
        // 设置租赁期限信息
        agreementVo.setLeaseTerm(leaseTerm);
        // 返回租约视图对象
        return agreementVo;
    }

    /**
     * 根据查询条件分页查询租约信息
     *
     * @param page    分页对象
     * @param queryVo 查询条件封装对象
     * @return 分页查询结果
     */
    @Override
    public IPage<AgreementVo> pageAgreementByQuery(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        // 调用Mapper方法进行分页查询
        return baseMapper.pageAgreementByQuery(page, queryVo);
    }
}
